<script setup lang="ts">
import { loginByQQ, getCode, bindMobile } from '@/api/user'
import { useSendMobileCode } from '@/composable/follow'
import { useUserStore } from '@/stores'
import type { User } from '@/types/user'
import { showSuccessToast, type FormInstance } from 'vant'
import type { FieldRule } from 'vant/lib/field/types'
import { onMounted, onUnmounted, ref } from 'vue'
import { useRouter } from 'vue-router'
// 登录成功
const store = useUserStore()
const router = useRouter()
//登录成功做的事
const loginSuccess = (res: { data: User }) => {
  store.setUser(res.data)
  router.replace(store.returnUrl || '/')
  showSuccessToast('登录成功')
  // store.setReturnUrl('')
}
//qq登录需要的id
const openId = ref('')
//要不要绑定
const isBind = ref(true)
const mobile = ref('')
const code = ref('')
//验证码
const { form, time, send } = useSendMobileCode(mobile.value, 'bindMobile')
//表单校验
const mobileRule: FieldRule[] = [
  { required: true, message: '手机号必填', trigger: 'onBlur' },
  { pattern: /^1[3-9]\d{9}$/, message: '手机号11位', trigger: 'onBlur' }
]

const codeRule: FieldRule[] = [
  { required: true, message: '验证码必填', trigger: 'onBlur' },
  { pattern: /^\d{6}$/, message: '验证长度不对', trigger: 'onBlur' }
]
onMounted(() => {
  if (window.QC.Login.check()) {
    window.QC.Login.getMe((id) => {
      openId.value = id
      // QQ，登录
      loginByQQ(id)
        .then((res) => {
          // 登录成功
          loginSuccess(res)
        })
        .catch(() => {
          // 登录失败
          isBind.value = false
        })
    })
  }
})
//绑定手机号
const bind = async () => {
  const res = await bindMobile({
    mobile: mobile.value,
    code: code.value,
    openId: openId.value
  })
  loginSuccess(res)
}
</script>

<template>
  <div class="login-page" v-if="!isBind">
    <cp-nav-bar></cp-nav-bar>
    <div class="login-head">
      <h3>手机绑定</h3>
    </div>
    <van-form autocomplete="off" @submit="bind" ref="form">
      <van-field
        v-model="mobile"
        name="mobile"
        :rules="mobileRule"
        placeholder="请输入手机号"
      ></van-field>
      <van-field
        v-model="code"
        name="code"
        :rules="codeRule"
        placeholder="请输入验证码"
      >
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">{{
            time > 0 ? `${time}s后再次发送` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-button
          block
          round
          type="primary"
          native-type="submit"
          @click="bind"
        >
          立即绑定
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
